package com.ssyt.hiring.company.service;

import java.util.ArrayList;
import java.util.List;

import com.ssyt.hiring.common.util.DBUtil;
import com.ssyt.hiring.company.entity.CompanyEntity;

public class CompanyService {

	public int register(CompanyEntity company) {
		String sql = "INSERT INTO company VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?,NULL,NULL)";
		List<Object> paramList = new ArrayList<>();
		paramList.add(company.getCode());
		paramList.add(company.getPassword());
		paramList.add(company.getName());
		paramList.add(company.getEmail());
		paramList.add(company.getPhone());
		paramList.add(company.getLinkman());
		paramList.add(company.getAddress());
		paramList.add(company.getNumbers());
		paramList.add(company.getSlogan());
		return DBUtil.executeUpdate(sql, paramList);
	}

	public CompanyEntity login(String code, String password) {
		String sql = "SELECT * FROM company WHERE code=? AND password=?";
		return DBUtil.getUniqueResult(CompanyEntity.class, sql, code, password);
	}

	public int updatePassword(int id, String password, String oldpassword) {
		String sql = "UPDATE company SET password=? where id=? and password=?";
		return DBUtil.executeUpdate(sql, password, id, oldpassword);
	}

	public CompanyEntity queryCompany(int id) {
		String sql = "Select * from company where id=?";
		return DBUtil.getUniqueResult(CompanyEntity.class, sql, id);
	}

	public int update(CompanyEntity companyEntity) {
		List<Object> params = new ArrayList<Object>();
		params.add(companyEntity.getName());
		params.add(companyEntity.getEmail());
		params.add(companyEntity.getPhone());
		params.add(companyEntity.getLinkman());
		params.add(companyEntity.getAddress());
		params.add(companyEntity.getNumbers());
		params.add(companyEntity.getSlogan());
		params.add(companyEntity.getId());

		String sql = "UPDATE company SET name = ?, email = ? , phone = ? , linkman= ? , address= ? , numbers= ? , slogan= ? where id=?";
		return DBUtil.executeUpdate(sql, params);
	}

	public CompanyEntity queryMail(String email) {
		String sql = "SELECT * FROM company WHERE email=?";
		return DBUtil.getUniqueResult(CompanyEntity.class, sql, email);
	}

	public int forgetpwd(int id, String password) {
		String sql = "UPDATE company SET password = ? WHERE id = ?";
		return DBUtil.executeUpdate(sql, password, id);
	}
}
